package com.rainbow.demo.action;


import com.rainbow.demo.service.IModifyService;
import com.rainbow.demo.service.IQueryService;
import com.rainbow.formework.annotation.HMKAutowired;
import com.rainbow.formework.annotation.HMKController;
import com.rainbow.formework.annotation.HMKRequestMapping;
import com.rainbow.formework.annotation.HMKRequestParam;
import com.rainbow.formework.webmvc.HMKModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 公布接口url
 * @author Tom
 *
 */
@HMKController
@HMKRequestMapping("/web")
public class MyAction {

	@HMKAutowired
	IQueryService queryService;
	@HMKAutowired
	IModifyService modifyService;
	
	@HMKRequestMapping("/query.json")
	public HMKModelAndView query(HttpServletRequest request, HttpServletResponse response,
								 @HMKRequestParam("name") String name){
		String result = queryService.query(name);
		System.out.println(result);
		return out(response,result);
	}
	
	@HMKRequestMapping("/add*.json")
	public HMKModelAndView add(HttpServletRequest request, HttpServletResponse response,
                              @HMKRequestParam("name") String name, @HMKRequestParam("addr") String addr){
		String result = modifyService.add(name,addr);
		return out(response,result);
	}
	
	@HMKRequestMapping("/remove.json")
	public HMKModelAndView remove(HttpServletRequest request, HttpServletResponse response,
                                 @HMKRequestParam("id") Integer id){
		String result = modifyService.remove(id);
		return out(response,result);
	}
	
	@HMKRequestMapping("/edit.json")
	public HMKModelAndView edit(HttpServletRequest request, HttpServletResponse response,
                               @HMKRequestParam("id") Integer id,
                               @HMKRequestParam("name") String name){
		String result = modifyService.edit(id,name);
		return out(response,result);
	}
	
	
	
	private HMKModelAndView out(HttpServletResponse resp, String str){
		try {
			resp.getWriter().write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
